package org.jeecg.modules.foc.service.impl;

import com.alibaba.fastjson.util.TypeUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import dm.jdbc.util.StringUtil;
import org.jeecg.modules.c.entity.MDevice;
import org.jeecg.modules.c.entity.MRoom;
import org.jeecg.modules.c.service.IMDeviceService;
import org.jeecg.modules.c.service.IMRoomService;
import org.jeecg.modules.concat.FocConcat;
import org.jeecg.modules.foc.entity.FocDevice;
import org.jeecg.modules.foc.entity.FocSpace;
import org.jeecg.modules.foc.entity.MDataConf;
import org.jeecg.modules.foc.mapper.FocDeviceMapper;
import org.jeecg.modules.foc.service.IFocDeviceService;
import org.jeecg.modules.foc.service.IFocSpaceService;
import org.jeecg.modules.foc.service.IMDataConfService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * @Description: 设备管理
 * @Author: jeecg-boot
 * @Date: 2025-01-10
 * @Version: V1.0
 */
@Service
@Lazy
public class FocDeviceServiceImpl extends ServiceImpl<FocDeviceMapper, FocDevice> implements IFocDeviceService {

    @Autowired
    private IFocSpaceService focSpaceService;
    @Autowired
    private IMDeviceService mDeviceService;
    @Autowired
    private IMRoomService mRoomService;
    @Autowired
    private IMDataConfService mDataConfService;


    @Override
    public void toDeviceConvert(FocDevice focDevice) {
        //如果设备id不为空，则进行转换
        if (StringUtil.isNotEmpty(focDevice.getDeviceid()) && StringUtil.isNotEmpty(focDevice.getName()) && StringUtil.isNotEmpty(focDevice.getDeviceType())) {
            MDevice oldDevice = mDeviceService.getById(focDevice.getDeviceid());
            //创建c即可设备对象
            MDevice mDevice = new MDevice();     //取设备id的第几位和第几位
            // String SUID = focDevice.getDeviceid().substring(FocConcat.SCID_LEN - 1, FocConcat.SCID_LEN + FocConcat.SUID_LEN - 1);
            //设备子对象如歌存在，并且设备id不为空，则取老设备的，否则生成
            if (oldDevice != null && StringUtil.isNotEmpty(oldDevice.getDeviceid())) {
                mDevice = oldDevice;
            } else {
                //生成设备i
                QueryWrapper<MDataConf> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("mp_big_type", focDevice.getMpBigType());
                queryWrapper.eq("mp_device_type", focDevice.getMpDeviceType());
                queryWrapper.last("limit 1");
                MDataConf one = mDataConfService.getOne(queryWrapper);
                mDevice.setDevicetype(one.getMpCode().substring(0, 3));
                String maxSellno = mDeviceService.getMaxSellno(mDevice.getDevicetype());
                String deviceId = "";
                if (maxSellno != null) {
                    String intIndex = maxSellno.substring(0, 1);
                    String strIndex = maxSellno.substring(1, 2);
                    if (TypeUtils.castToInt(intIndex) < 9) {
                        intIndex = String.valueOf(TypeUtils.castToInt(intIndex) + 1);
                    } else {
                        intIndex = "0";
                        int intStrindex = (int) TypeUtils.castToChar(strIndex);
                        strIndex = String.valueOf((char)(intStrindex + 1));

                    }
                    deviceId = mDevice.getDevicetype() + intIndex + strIndex;
                } else {
                    deviceId = mDevice.getDevicetype() + "0" + "A";
                }
                mDevice.setDeviceid(deviceId);
            }
            mDevice.setScid(FocConcat.SCID);
            mDevice.setSuid(FocConcat.SUID);
            mDevice.setDevassestid(focDevice.getDeviceid());
            //生成一个两位设备序号，第一位数字0-9，第二位数字A-Z
            mDevice.setDevicename(focDevice.getName());
            // 将设备信息赋值给mDevice对象
            if (StringUtil.isNotEmpty(focDevice.getParentGuid())) {
                FocSpace roomSpec = focSpaceService.getOne(new QueryWrapper<FocSpace>().eq("guid", focDevice.getParentGuid()));
                mDevice.setRoomid(roomSpec.getRoomid());
                //如果roomid不为空，咋使用本身的roomid
                if (StringUtil.isNotEmpty(focDevice.getRoomid())) {
                    mDevice.setRoomid(focDevice.getRoomid());
                    //判断站点iD是否存在，如果不存在，则插入
                }
                FocSpace sideSPec = focSpaceService.getOne(new QueryWrapper<FocSpace>().eq("guid", roomSpec.getParentGuid()));
                mDevice.setSiteid(sideSPec.getSiteid());
                //补充站点数据
                if (StringUtil.isNotEmpty(focDevice.getRoomid())) {
                    MRoom mRoom = new MRoom();
                    mRoom.setScid(FocConcat.SCID);
                    mRoom.setRoomid(focDevice.getRoomid());
                    mRoom.setRoomname(focDevice.getRoomName());
                    mRoom.setSiteid(mDevice.getSiteid());
                    mRoomService.saveOrUpdate(mRoom);
                }
            }
            mDeviceService.saveOrUpdate(mDevice);
            focDevice.setDeviceNewId(mDevice.getDeviceid());
            this.updateById(focDevice);
        }
    }
}
